<template>
  <div class="container">
    <div class="login">
      <div class="title">欢迎登录助农服务平台</div>
      <el-form
        :model="registerParam"
        label-width="auto"
        style="max-width: 600px"
        :rules="rules"
        ref="form"
      >
        <el-form-item prop="userName">
          <el-input
            v-model="registerParam.userName"
            prefix-icon="user"
            placeholder="请输入账号"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="registerParam.password"
            prefix-icon="lock"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item prop="confirmPass">
          <el-input
            v-model="registerParam.confirmPass"
            prefix-icon="lock"
            placeholder="请确认密码"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button
            style="
              width: 100%;
              background-color: rgb(40, 130, 107);
              border-color: rgb(40, 130, 107);
              color: white;
            "
            @click="register"
            ><span>注 册</span></el-button
          >
        </el-form-item>
        <div class="register">
          <div></div>
          <div id="reg2">已有账号？请 <a href="/login">登录</a></div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts" name="register">
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { reqUserRegister } from "@/api/user";
import { useRouter } from "vue-router";
//获取路由器对象
let $router = useRouter();

// 注册表单
let registerParam = reactive({
  userName: "",
  password: "",
  confirmPass: "",
});

//获取form组件实例
let form = ref<any>();

// 点击注册按钮回调
const register = async () => {
  //保证表单校验两项都复合条件
  await form.value.validate();
  try {
    let param = Object.assign(
      {},
      { userName: registerParam.userName, password: registerParam.password }
    );
    let result = await reqUserRegister(param);
    if (result.code == 200) {
      $router.push("/login");
    }else {
      ElMessage.error(result.msg)
    }
  } catch (error) {
    ElMessage({
      type: "error",
      message: (error as Error).message,
    });
  }
};

const validatorPassword = (rule: any, value: any, callBack: any) => {
  //rule:即为表单校验规则对象
  //value:即为当前文本的内容
  //callBack:回调函数
  // 密码至少包含字母、数字，1-9位
  if (/^(?=.*[a-zA-Z])(?=.*\d).{1,9}$/.test(value)) {
    callBack();
  } else {
    callBack(new Error("密码至少包含字母、数字，1-9位"));
  }
};
// 确认密码
const validatorConfirmPassword = (rule: any, value: any, callBack: any) => {
  if (value == registerParam.password) {
    callBack();
  } else {
    callBack(new Error("两次输入的密码不一致，请重新输入"));
  }
};
// 表单校验规则
const rules = {
  userName: [
    {
      required: true,
      message: "请输入用户名,最少为一个字符",
      trigger: "blur",
      min: 1,
    },
  ],
  password: [{ trigger: "blur", validator: validatorPassword }],
  confirmPass: [{ trigger: "change", validator: validatorConfirmPassword }],
};
</script>

<style scoped lang="scss">
.container {
  height: 100vh;
  overflow: hidden;
  background-image: url("./images/bg.png");
  background-size: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
  .login {
    width: 400px;
    padding: 30px;
    background-color: white;
    border-radius: 5px;
    .title {
      text-align: center;
      font-size: 20px;
      margin-bottom: 20px;
      color: rgb(51, 51, 51);
    }
    .register {
      display: flex;
      div {
        flex: 1;
      }
      #reg2 {
        text-align: right;
        a {
          text-decoration: none;
        }
      }
    }
  }
}
</style>
